package cn.anquing.datastructure.util;

import cn.anquing.datastructure.sort.BubbleSorter;
import cn.anquing.datastructure.sort.ISort;

import java.util.Random;

/**
 * <pre>
 *
 * @description
 * @author wangqq
 * @email wangqq65@gdii-yueyun.com
 * @since 2022/5/17 15:23
 * @copyright 广东美云智数科技有限公司
 * </pre>
 */
public class SortTestHelper {

    /**
     * 生成随机数集合
     * @param n
     * @param rangeL
     * @param rangeR
     * @return
     */
    public static Integer[] generateRandomArray(int n,int rangeL,int rangeR){
        Integer[] arr = new Integer[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (int)(Math.random() * (rangeR - rangeL + 1)) + rangeL;
        }
        return arr;
    }

    /**
     * 验证是否排序成功
     * @param arr
     * @return
     */
    public static  boolean isSorted(Integer[] arr){
        for (int i = 0;i< arr.length-1;i++) {
            if (arr[i] > arr[i+1] ) {
                return false;
            }
        }
        return true;
    }

    public static void testSort(String sortName,ISort iSort,Integer[] arr){
        long start = System.currentTimeMillis();
        iSort.sortInt(arr);
        long end = System.currentTimeMillis();
        System.out.println("使用"+sortName+"排序方法，花费时间：" + (end - start) + "毫秒");
    }

    public static void main(String[] args) {
        Integer[] integers = SortTestHelper.generateRandomArray(10000, 100, 100000000);
        BubbleSorter bubbleSorter = new BubbleSorter();
        SortTestHelper.testSort("选择排序法",bubbleSorter,integers);
        boolean sorted = SortTestHelper.isSorted(integers);
        System.out.println("排序结果："+ (sorted ? "成功" : "失败"));
    }
}
